package fight.view
{
	import fight.FightC;
	import flash.ui.Keyboard;
	import org.airmvc.Dispatcher;
	
	import interfaces.IOperate;
	
	import morn.core.components.Clip;
	
	import ui.fight.ControlPanelUI;
	
	import vo.GeneralVO;

	public class ControlPanel extends ControlPanelUI implements IOperate
	{
		public function ControlPanel()
		{
			this.x = 150;
			this.y = 310;
			
			itemList = [];
			for(var i:int;i<4;i++)
			{
				var item:ControlItem = new ControlItem();
				item.x = 175;
				item.y = i * 32;
				addChild(item);
				itemList.push(item);
			}
			
			arrow = new Clip("png.common.clip_arrow", 1, 2);
			arrow.mouseChildren = false;
			arrow.mouseEnabled = false;
			arrow.frame = 1;
			arrow.x = 105;
			addChild(arrow);
		}
		
		public function update(g:GeneralVO):void
		{
			btStrength.text = String(g.strength);
			btPower.text = String(g.power);
			btKnowledge.text = String(g.knowledge);
			btLoyalty.text = String(g.loyalty);
			
			for each(var item:ControlItem in itemList)
			{
				item.index = 0;
			}
			
			index = 0;
		}
		
		public function execute(keyCode:int):void
		{
			switch(keyCode)
			{
				case Keyboard.A:
					curItem.index--;
					break;
				case Keyboard.D:
					curItem.index++;
					break;
				case Keyboard.W:
					index--;
					break;
				case Keyboard.S:
					index++;
					break;
				case Keyboard.SPACE:
					var arr:Array = [];
					for each(var item:ControlItem in itemList)
					{
						arr.push(item.index);
					}
					Dispatcher.send(FightC.GIVE_ORDER,this,arr);
					break;
			}
		}
		
		public function get index():int
		{
			return _index;
		}
		
		public function set index(value:int):void
		{
			if(value < 0 || value > 3)
				return;
			
			curItem = itemList[value];
			//arrow.x = curItem.x - 8;
			arrow.y = curItem.y + 4;
			_index = value;
		}
		
		private var arrow:Clip;
		private var _index:int;
		private var itemList:Array
		private var curItem:ControlItem;
	}
}